/*
 * 文件名：MenuAction.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：qianfm
 * 修改时间：Sep 25, 2012
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.manager.action;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.wootide.cms.common.manager.BeanFactory;
import com.wootide.cms.common.manager.Constants;
import com.wootide.cms.common.manager.GeneralUtils;
import com.wootide.cms.common.manager.SHException;
import com.wootide.cms.manager.pojo.User;
import com.wootide.cms.manager.service.IUserService;

/**
 * 〈菜单管理〉
 * 〈功能详细描述〉
 * @author qianfm
 * @version Sep 25, 2012
 * @see LoginAction
 * @since
 */

public class LoginAction extends BaseAction
{
    /**
     * 日志记录
     */
    private static final Logger LOGGER = Logger.getLogger(LoginAction.class);
    
    private User user;
    
    private IUserService userService;
    
    /*public IRoleOperaService roleOperaService;*/
    
    private Map<String, Object> paramMap;
    
    /**
     * 菜单集合
     */
    //private List<Menu> menuList = new ArrayList<Menu>();
    
    /**
     * 初始化业务类
     * Description: <br>
     * Implement: <br>
     * @throws SHException
     * @see
     * @return void
     * @author qianfm
     */
    private void initService() throws SHException
    {
        try
        {
            if (GeneralUtils.isNull(userService))
            {
                userService = (IUserService)BeanFactory.getBean("userService");
                // roleOperaService = (IRoleOperaService)BeanFactory.getBean("roleOperaService");
            }
            paramMap = new LinkedHashMap<String, Object>();
        }
        catch (Exception e)
        {
            LOGGER.error("初始化业务类失败！", e);
            throw new SHException("初始化业务类失败！", e);
        }
    }
    
    /**
     * 登陆
     * Description: <br>
     * Implement: <br>
     * @return
     * @throws SHException
     * @see
     * @return String
     * @author qianfm
     */
    public String login() throws SHException
    {
        // 初始化业务服务
        initService();
        
        if (GeneralUtils.isNotNull(user))
        {
            //paramMap.put("userName", user.getUserName());
            User userObj = userService.getUser(user);
            if (GeneralUtils.isNotNull(userObj))
            {
                if (StringUtils.equals(userObj.getUserName(), user.getUserName())
                        &&StringUtils.equals(userObj.getUserPwd(), user.getUserPwd())
                        && userObj.getUserState() == 0)//当前登录用户和密码验证，以及该用户状态是否启用
                {
                    this.getRequest().getSession()
                            .setAttribute(Constants.LOGIN_USER, userObj);
                    
                    //获取菜单目录
                    //getMenus();
                    //ActionContext.getContext().put("menuList", menuList);
                    return "success";
                }
                else
                {
                	this.getRequest().setAttribute("errorMsg", "密码有误");
                    return "login_failure";
                }
                
            }
            else
            {
            	this.getRequest().setAttribute("errorMsg", "用户名有误");
                return "login_failure";
            }
            
        }
        return "success";
    }
    
    /**
     * Description: 用户退出系统<br>
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author Administrator
     */
    public String logout()
    {
        if (GeneralUtils.isNotNull(this.getRequest().getSession()))
        {
            this.getRequest().getSession().invalidate();
        }else{
            return ERROR;
        }
        return SUCCESS;
    }
    
    /**
     * Description: 跳转到首页<br>
     * Implement: <br>
     * @return
     * @see
     * @return String
     * @author Administrator
     */
    public String index()
    {
        //..
        return SUCCESS;
    }
    
    /*public IRoleOperaService getRoleOperaService()
    {
        return roleOperaService;
    }
    
    public void setRoleOperaService(IRoleOperaService roleOperaService)
    {
        this.roleOperaService = roleOperaService;
    }*/
    
    /**
     * 根据用户权限获取相应的菜单
     * Description: <br>
     * Implement: <br>
     * @throws SHException
     * @see
     * @return void
     * @author qianfm
     */
    public void getMenus() throws SHException
    {
        /*
         * 描述：工具当前用户获取相应权限的所有子菜单
         * 前提：获取user的ID
         */
        User userTemp = (User)getRequest().getSession()
                .getAttribute(Constants.LOGIN_USER);
        //获取到当前用户相关联的可以有权限访问的“子菜单”目录
       // menuList = roleOperaService.getMenus(userTemp.getUserId());
        
        /**
         * time:2012年10月18日11:28:08
         * 说明：根据数据tbl_menu表中字段列名meng_code的命名规则，
         * 自动判断当前登录用户所有权限下的菜单目录
         */
        /* String menuIdTemp ;
        for(Menu mm1:menus){//获取一级菜单
            menuIdTemp = "";
            if(mm1.getMenuParent().toString().equals("0")){
                menuIdTemp += mm1.getMenuId();
                System.out.println("--"+mm1.getMenuName());
                for(Menu mm2:menus){//通过一级菜单，得到相应的二级菜单列表
                    if(mm2.getMenuParent().toString().equals(menuIdTemp)){
                        System.out.println("----"+mm2.getMenuName());
                    }
                }
            }
        } */
        //-------------------------
        //getRequest().getSession().setAttribute("menuList", menus);
//        return "menu";
    }
    
    public IUserService getUserService()
    {
        return userService;
    }
    
    public void setUserService(IUserService userService)
    {
        this.userService = userService;
    }
    
    public Map<String, Object> getParamMap()
    {
        return paramMap;
    }
    
    public void setParamMap(Map<String, Object> paramMap)
    {
        this.paramMap = paramMap;
    }
    
    public User getUser()
    {
        return user;
    }
    
    public void setUser(User user)
    {
        this.user = user;
    }

    /*public List<Menu> getMenuList()
    {
        return menuList;
    }

    public void setMenuList(List<Menu> menuList)
    {
        this.menuList = menuList;
    }*/
    
}
